<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: src/tree_algorithms/searchtree/searchtree.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('searchtree_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">searchtree.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="searchtree_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright 2016- huaxz &lt;huaxz1986@163.com&gt;</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * This program is free software: you can redistribute it and/or modify</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * the Free Software Foundation, either version 2 of the License, or</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> * (at your option) any later version.</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> * This program is distributed in the hope that it will be useful,</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * GNU General Public License for more details.</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * You should have received a copy of the GNU General Public License</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"> * Author: huaxz1986@163.com (huaxz)</span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#ifndef SEARCHTREE</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="preprocessor">#define SEARCHTREE</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#include&quot;../binarytree/binarytree.h&quot;</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;{</div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    <span class="keyword">namespace </span>TreeAlgorithm</div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;    {</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div>
<div class="line"><a name="l00033"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html">   33</a></span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> NodeType&gt; <span class="keyword">class </span><a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html">SearchTree</a>:<span class="keyword">public</span> <a class="code" href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html">BinaryTree</a>&lt;NodeType&gt;</div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;        {</div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;    <span class="keyword">public</span>:</div>
<div class="line"><a name="l00036"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a0a9b2de6326042f2a1fa8a3a0def67ab">   36</a></span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> NodeType::KeyType <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a0a9b2de6326042f2a1fa8a3a0def67ab">T</a>; </div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;</div>
<div class="line"><a name="l00048"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#affaf44d28dc2180f93669cb8d6c09c84">   48</a></span>&#160;            std::shared_ptr&lt;NodeType&gt; <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#affaf44d28dc2180f93669cb8d6c09c84">search</a>(<span class="keyword">const</span> T&amp; value,std::shared_ptr&lt;NodeType&gt; node=std::shared_ptr&lt;NodeType&gt;())</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;            {</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;                <span class="keywordflow">if</span>(!node) node=this-&gt;<a class="code" href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html#aa0d43af70ef50ff59740dced2e832074">root</a>;</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;                <span class="keywordflow">while</span>(node&amp;&amp;node-&gt;key!=value)</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;                {</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;                    <span class="keywordflow">if</span>(value&lt;node-&gt;key)</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;                    {</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;                        node=node-&gt;lchild;</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;                    }<span class="keywordflow">else</span></div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;                    {</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;                        node=node-&gt;rchild;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;                    }</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;                }</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;                <span class="keywordflow">return</span> node;</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;            }</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;</div>
<div class="line"><a name="l00075"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#af0bb418896808f72bb1edee55d238d55">   75</a></span>&#160;            std::shared_ptr&lt;NodeType&gt; <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#af0bb418896808f72bb1edee55d238d55">min</a>(std::shared_ptr&lt;NodeType&gt; node)</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;            {</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;                <span class="keywordflow">if</span>(!node)</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;                {</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;min() should not be aplied on nullptr&quot;</span>);</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;                }</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;                <span class="keywordflow">while</span>(node-&gt;lchild)</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;                {</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;                    node=node-&gt;lchild;</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;                }</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;                <span class="keywordflow">return</span> node;</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;            }</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;</div>
<div class="line"><a name="l00099"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a2dfe32f3914b03ccdb22f88f24a6afdd">   99</a></span>&#160;            std::shared_ptr&lt;NodeType&gt; <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a2dfe32f3914b03ccdb22f88f24a6afdd">max</a>(std::shared_ptr&lt;NodeType&gt; node)</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;            {</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;                <span class="keywordflow">if</span>(!node)</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;                {</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;max() should not be aplied on nullptr&quot;</span>);</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;                }</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;                <span class="keywordflow">while</span>(node-&gt;rchild)</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;                {</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;                    node=node-&gt;rchild;</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;                }</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;                <span class="keywordflow">return</span> node;</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;            }</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;</div>
<div class="line"><a name="l00127"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a9d53e1f0b130263f2974dced515aa1f1">  127</a></span>&#160;            std::shared_ptr&lt;NodeType&gt; <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a9d53e1f0b130263f2974dced515aa1f1">successor</a>(std::shared_ptr&lt;NodeType&gt; node)</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;            {</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;                <span class="keywordflow">if</span>(!node)</div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;                {</div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;successor() should not be aplied on nullptr&quot;</span>);</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;                }</div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;                <span class="keywordflow">if</span>(node-&gt;rchild) <span class="comment">//以右子节点为根的子树中的最小值节点就是`node`的后继节点</span></div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;                {</div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;                    <span class="keywordflow">return</span> <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#af0bb418896808f72bb1edee55d238d55">min</a>(node-&gt;rchild);</div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;                }</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;                <span class="keyword">auto</span> shared_p=node-&gt;parent.lock();  <span class="comment">//取出父节点</span></div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;                <span class="keywordflow">while</span>(shared_p &amp;&amp; node-&gt;is_right_child())  <span class="comment">//向上迭代，直到父节点为空或者node是父节点的左子节点，则迭代终止</span></div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;                {</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;                    node=shared_p;</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;                    shared_p=shared_p-&gt;parent.lock();</div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;                }</div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;                <span class="keywordflow">return</span> shared_p;</div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;            }</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;</div>
<div class="line"><a name="l00161"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a0e767d97590d19c97d595708d4419fe1">  161</a></span>&#160;            std::shared_ptr&lt;NodeType&gt;<a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a0e767d97590d19c97d595708d4419fe1">predecesor</a>(std::shared_ptr&lt;NodeType&gt; node)</div>
<div class="line"><a name="l00162"></a><span class="lineno">  162</span>&#160;            {</div>
<div class="line"><a name="l00163"></a><span class="lineno">  163</span>&#160;                <span class="keywordflow">if</span>(!node)</div>
<div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;                {</div>
<div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;predecesor() should not be aplied on nullptr&quot;</span>);</div>
<div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;                }</div>
<div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;                <span class="keywordflow">if</span>(node-&gt;lchild) <span class="comment">//以左子节点为根的子树中的最大值节点就是`node`的前驱节点</span></div>
<div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;                {</div>
<div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;                    <span class="keywordflow">return</span> <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a2dfe32f3914b03ccdb22f88f24a6afdd">max</a>(node-&gt;lchild);</div>
<div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;                }</div>
<div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;                <span class="keyword">auto</span> shared_p=node-&gt;parent.lock();<span class="comment">//取出父节点</span></div>
<div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;                <span class="keywordflow">while</span>(shared_p&amp;&amp;node-&gt;is_left_child())<span class="comment">//向上迭代，直到父节点为空或者node是父节点的右子节点，则迭代终止</span></div>
<div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;                {</div>
<div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;                    node=shared_p;</div>
<div class="line"><a name="l00175"></a><span class="lineno">  175</span>&#160;                    shared_p=shared_p-&gt;parent.lock();</div>
<div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;                }</div>
<div class="line"><a name="l00177"></a><span class="lineno">  177</span>&#160;                <span class="keywordflow">return</span> shared_p;</div>
<div class="line"><a name="l00178"></a><span class="lineno">  178</span>&#160;            }</div>
<div class="line"><a name="l00180"></a><span class="lineno">  180</span>&#160;</div>
<div class="line"><a name="l00191"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a83724f0079d16419d4cceba74602cee1">  191</a></span>&#160;            <span class="keywordtype">void</span> <a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a83724f0079d16419d4cceba74602cee1">insert</a>(std::shared_ptr&lt;NodeType&gt; node)</div>
<div class="line"><a name="l00192"></a><span class="lineno">  192</span>&#160;            {</div>
<div class="line"><a name="l00193"></a><span class="lineno">  193</span>&#160;                node-&gt;lchild=std::shared_ptr&lt;NodeType&gt;();</div>
<div class="line"><a name="l00194"></a><span class="lineno">  194</span>&#160;                node-&gt;rchild=std::shared_ptr&lt;NodeType&gt;();</div>
<div class="line"><a name="l00195"></a><span class="lineno">  195</span>&#160;</div>
<div class="line"><a name="l00196"></a><span class="lineno">  196</span>&#160;                <span class="keywordflow">if</span>(!this-&gt;<a class="code" href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html#aa0d43af70ef50ff59740dced2e832074">root</a>)</div>
<div class="line"><a name="l00197"></a><span class="lineno">  197</span>&#160;                {</div>
<div class="line"><a name="l00198"></a><span class="lineno">  198</span>&#160;                    this-&gt;<a class="code" href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html#aa0d43af70ef50ff59740dced2e832074">root</a>=node;</div>
<div class="line"><a name="l00199"></a><span class="lineno">  199</span>&#160;                    <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00200"></a><span class="lineno">  200</span>&#160;                }</div>
<div class="line"><a name="l00201"></a><span class="lineno">  201</span>&#160;                <span class="keywordflow">else</span></div>
<div class="line"><a name="l00202"></a><span class="lineno">  202</span>&#160;                {</div>
<div class="line"><a name="l00203"></a><span class="lineno">  203</span>&#160;                    <span class="keyword">auto</span> temp=this-&gt;<a class="code" href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html#aa0d43af70ef50ff59740dced2e832074">root</a>;</div>
<div class="line"><a name="l00204"></a><span class="lineno">  204</span>&#160;                    <span class="keyword">auto</span> temp_parent=std::shared_ptr&lt;NodeType&gt;();</div>
<div class="line"><a name="l00205"></a><span class="lineno">  205</span>&#160;                    <span class="keywordtype">bool</span> left=<span class="keyword">true</span>;</div>
<div class="line"><a name="l00206"></a><span class="lineno">  206</span>&#160;                    <span class="keywordflow">while</span>(temp)  <span class="comment">//遍历</span></div>
<div class="line"><a name="l00207"></a><span class="lineno">  207</span>&#160;                    {</div>
<div class="line"><a name="l00208"></a><span class="lineno">  208</span>&#160;                        temp_parent=temp;         <span class="comment">//指向父节点</span></div>
<div class="line"><a name="l00209"></a><span class="lineno">  209</span>&#160;                        <span class="keywordflow">if</span>(node-&gt;key&lt;temp-&gt;key)   <span class="comment">//向左侧遍历</span></div>
<div class="line"><a name="l00210"></a><span class="lineno">  210</span>&#160;                        {</div>
<div class="line"><a name="l00211"></a><span class="lineno">  211</span>&#160;                            temp=temp-&gt;lchild;</div>
<div class="line"><a name="l00212"></a><span class="lineno">  212</span>&#160;                            left=<span class="keyword">true</span>;</div>
<div class="line"><a name="l00213"></a><span class="lineno">  213</span>&#160;                        }</div>
<div class="line"><a name="l00214"></a><span class="lineno">  214</span>&#160;                        <span class="keywordflow">else</span></div>
<div class="line"><a name="l00215"></a><span class="lineno">  215</span>&#160;                        {</div>
<div class="line"><a name="l00216"></a><span class="lineno">  216</span>&#160;                            temp=temp-&gt;rchild;    <span class="comment">//向右侧遍历</span></div>
<div class="line"><a name="l00217"></a><span class="lineno">  217</span>&#160;                            left=<span class="keyword">false</span>;</div>
<div class="line"><a name="l00218"></a><span class="lineno">  218</span>&#160;                        }</div>
<div class="line"><a name="l00219"></a><span class="lineno">  219</span>&#160;                    }</div>
<div class="line"><a name="l00220"></a><span class="lineno">  220</span>&#160;                    <span class="comment">//现在temp为空，node挂载到temp_parent下</span></div>
<div class="line"><a name="l00221"></a><span class="lineno">  221</span>&#160;                    node-&gt;parent=temp_parent;</div>
<div class="line"><a name="l00222"></a><span class="lineno">  222</span>&#160;                    <span class="keywordflow">if</span>(left) temp_parent-&gt;lchild=node;</div>
<div class="line"><a name="l00223"></a><span class="lineno">  223</span>&#160;                    <span class="keywordflow">else</span> temp_parent-&gt;rchild=node;</div>
<div class="line"><a name="l00224"></a><span class="lineno">  224</span>&#160;                }</div>
<div class="line"><a name="l00225"></a><span class="lineno">  225</span>&#160;</div>
<div class="line"><a name="l00226"></a><span class="lineno">  226</span>&#160;            }</div>
<div class="line"><a name="l00228"></a><span class="lineno">  228</span>&#160;</div>
<div class="line"><a name="l00250"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a3b59a0c117766590f7f01027ad0fbfff">  250</a></span>&#160;            <span class="keywordtype">void</span> <span class="keyword">remove</span>(std::shared_ptr&lt;NodeType&gt; node)</div>
<div class="line"><a name="l00251"></a><span class="lineno">  251</span>&#160;            {</div>
<div class="line"><a name="l00252"></a><span class="lineno">  252</span>&#160;                <span class="keywordflow">if</span>(!node)</div>
<div class="line"><a name="l00253"></a><span class="lineno">  253</span>&#160;                {</div>
<div class="line"><a name="l00254"></a><span class="lineno">  254</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;node removed is nullptr&quot;</span>);</div>
<div class="line"><a name="l00255"></a><span class="lineno">  255</span>&#160;                }</div>
<div class="line"><a name="l00256"></a><span class="lineno">  256</span>&#160;                <span class="comment">//*************  判定 node 必须位于树中 ***********</span></div>
<div class="line"><a name="l00257"></a><span class="lineno">  257</span>&#160;                <span class="keyword">auto</span> temp_node=this-&gt;<a class="code" href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html#aa0d43af70ef50ff59740dced2e832074">root</a>;</div>
<div class="line"><a name="l00258"></a><span class="lineno">  258</span>&#160;                <span class="keywordflow">while</span>(temp_node&amp;&amp; temp_node.get()!=node.get())</div>
<div class="line"><a name="l00259"></a><span class="lineno">  259</span>&#160;                {</div>
<div class="line"><a name="l00260"></a><span class="lineno">  260</span>&#160;                    <span class="keywordflow">if</span>(node-&gt;key&lt;temp_node-&gt;key)temp_node=temp_node-&gt;lchild;</div>
<div class="line"><a name="l00261"></a><span class="lineno">  261</span>&#160;                    <span class="keywordflow">else</span> temp_node=temp_node-&gt;rchild;</div>
<div class="line"><a name="l00262"></a><span class="lineno">  262</span>&#160;                }</div>
<div class="line"><a name="l00263"></a><span class="lineno">  263</span>&#160;                <span class="keywordflow">if</span>(temp_node.get()!=node.get())</div>
<div class="line"><a name="l00264"></a><span class="lineno">  264</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;node removed must be in the tree&quot;</span>);</div>
<div class="line"><a name="l00265"></a><span class="lineno">  265</span>&#160;</div>
<div class="line"><a name="l00266"></a><span class="lineno">  266</span>&#160;                <span class="comment">//************** 执行删除过程  *****************</span></div>
<div class="line"><a name="l00267"></a><span class="lineno">  267</span>&#160;                    <span class="keywordflow">if</span>((!node-&gt;lchild)&amp;&amp;(!node-&gt;rchild)) <span class="comment">//node是个叶子节点</span></div>
<div class="line"><a name="l00268"></a><span class="lineno">  268</span>&#160;                    {</div>
<div class="line"><a name="l00269"></a><span class="lineno">  269</span>&#160;                        <span class="keyword">auto</span> shared_p=node-&gt;parent.lock();</div>
<div class="line"><a name="l00270"></a><span class="lineno">  270</span>&#160;                        <span class="keywordflow">if</span>(shared_p) <span class="comment">//父节点非空</span></div>
<div class="line"><a name="l00271"></a><span class="lineno">  271</span>&#160;                        {</div>
<div class="line"><a name="l00272"></a><span class="lineno">  272</span>&#160;                            <span class="keywordflow">if</span>(node-&gt;is_left_child()) shared_p-&gt;lchild=std::shared_ptr&lt;NodeType&gt;();</div>
<div class="line"><a name="l00273"></a><span class="lineno">  273</span>&#160;                            <span class="keywordflow">if</span>(node-&gt;is_right_child()) shared_p-&gt;rchild=std::shared_ptr&lt;NodeType&gt;();</div>
<div class="line"><a name="l00274"></a><span class="lineno">  274</span>&#160;                        }<span class="keywordflow">else</span></div>
<div class="line"><a name="l00275"></a><span class="lineno">  275</span>&#160;                        {</div>
<div class="line"><a name="l00276"></a><span class="lineno">  276</span>&#160;                            this-&gt;<a class="code" href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html#aa0d43af70ef50ff59740dced2e832074">root</a>=std::shared_ptr&lt;NodeType&gt;();</div>
<div class="line"><a name="l00277"></a><span class="lineno">  277</span>&#160;                        }</div>
<div class="line"><a name="l00278"></a><span class="lineno">  278</span>&#160;                    }<span class="keywordflow">else</span> <span class="keywordflow">if</span>(node-&gt;lchild&amp;&amp; !node-&gt;rchild) <span class="comment">//node有左子，但是没有右子</span></div>
<div class="line"><a name="l00279"></a><span class="lineno">  279</span>&#160;                    {</div>
<div class="line"><a name="l00280"></a><span class="lineno">  280</span>&#160;                        <a class="code" href="namespace_introduction_to_algorithm_1_1_tree_algorithm.html#a4fb6d9c7d8b75a8d6ec4ee79fc0b1de5">transplant</a>(node-&gt;lchild,node,this-&gt;root);  <span class="comment">//左子剪切到node位置</span></div>
<div class="line"><a name="l00281"></a><span class="lineno">  281</span>&#160;                    }<span class="keywordflow">else</span> <span class="keywordflow">if</span>(node-&gt;rchild&amp;&amp; !node-&gt;lchild)<span class="comment">//node有右子，但是没有左子</span></div>
<div class="line"><a name="l00282"></a><span class="lineno">  282</span>&#160;                    {</div>
<div class="line"><a name="l00283"></a><span class="lineno">  283</span>&#160;                        <a class="code" href="namespace_introduction_to_algorithm_1_1_tree_algorithm.html#a4fb6d9c7d8b75a8d6ec4ee79fc0b1de5">transplant</a>(node-&gt;rchild,node,this-&gt;root);<span class="comment">//右子剪切到node位置</span></div>
<div class="line"><a name="l00284"></a><span class="lineno">  284</span>&#160;                    }<span class="keywordflow">else</span>                                    <span class="comment">//node既有左子，又有右子</span></div>
<div class="line"><a name="l00285"></a><span class="lineno">  285</span>&#160;                    {</div>
<div class="line"><a name="l00286"></a><span class="lineno">  286</span>&#160;                        <span class="keyword">auto</span> next_node=<a class="code" href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a9d53e1f0b130263f2974dced515aa1f1">successor</a>(node);      <span class="comment">//node的后继节点</span></div>
<div class="line"><a name="l00287"></a><span class="lineno">  287</span>&#160;                        <span class="keywordflow">if</span>(next_node!=node-&gt;rchild)   <span class="comment">//node后继不是node的右子，则转换成node的后继是node的右子情形</span></div>
<div class="line"><a name="l00288"></a><span class="lineno">  288</span>&#160;                        {</div>
<div class="line"><a name="l00289"></a><span class="lineno">  289</span>&#160;                             <a class="code" href="namespace_introduction_to_algorithm_1_1_tree_algorithm.html#a4fb6d9c7d8b75a8d6ec4ee79fc0b1de5">transplant</a>(next_node-&gt;rchild,next_node,this-&gt;root); <span class="comment">//将后继的右子剪切到后继的位置</span></div>
<div class="line"><a name="l00290"></a><span class="lineno">  290</span>&#160;                             next_node-&gt;rchild=node-&gt;rchild;  <span class="comment">//后继的右子设为node的右子</span></div>
<div class="line"><a name="l00291"></a><span class="lineno">  291</span>&#160;                             node-&gt;rchild-&gt;parent=next_node;   <span class="comment">//后继的右子设为node的右子</span></div>
<div class="line"><a name="l00292"></a><span class="lineno">  292</span>&#160;                             node-&gt;rchild=next_node;           <span class="comment">//后继的右子设为node的右子</span></div>
<div class="line"><a name="l00293"></a><span class="lineno">  293</span>&#160;                        }</div>
<div class="line"><a name="l00294"></a><span class="lineno">  294</span>&#160;                        <span class="comment">//node后继是node的右子</span></div>
<div class="line"><a name="l00295"></a><span class="lineno">  295</span>&#160;                        <a class="code" href="namespace_introduction_to_algorithm_1_1_tree_algorithm.html#a4fb6d9c7d8b75a8d6ec4ee79fc0b1de5">transplant</a>(node-&gt;rchild,node,this-&gt;root);</div>
<div class="line"><a name="l00296"></a><span class="lineno">  296</span>&#160;                        next_node-&gt;lchild=node-&gt;lchild;</div>
<div class="line"><a name="l00297"></a><span class="lineno">  297</span>&#160;                        node-&gt;lchild-&gt;parent=next_node;</div>
<div class="line"><a name="l00298"></a><span class="lineno">  298</span>&#160;                    }</div>
<div class="line"><a name="l00299"></a><span class="lineno">  299</span>&#160;            }</div>
<div class="line"><a name="l00300"></a><span class="lineno">  300</span>&#160;        };</div>
<div class="line"><a name="l00301"></a><span class="lineno">  301</span>&#160;    }</div>
<div class="line"><a name="l00302"></a><span class="lineno">  302</span>&#160;}</div>
<div class="line"><a name="l00303"></a><span class="lineno">  303</span>&#160;</div>
<div class="line"><a name="l00304"></a><span class="lineno">  304</span>&#160;</div>
<div class="line"><a name="l00305"></a><span class="lineno">  305</span>&#160;<span class="preprocessor">#endif // SEARCHTREE</span></div>
<div class="line"><a name="l00306"></a><span class="lineno">  306</span>&#160;</div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree_html"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html">IntroductionToAlgorithm::TreeAlgorithm::BinaryTree</a></div><div class="ttdoc">BinaryTree：二叉树，算法导论10章10.4节 </div><div class="ttdef"><b>Definition:</b> <a href="binarytree_8h_source.html#l00032">binarytree.h:32</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html_a83724f0079d16419d4cceba74602cee1"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a83724f0079d16419d4cceba74602cee1">IntroductionToAlgorithm::TreeAlgorithm::SearchTree::insert</a></div><div class="ttdeci">void insert(std::shared_ptr&lt; NodeType &gt; node)</div><div class="ttdoc">insert:向二叉搜索树中插入节点。 </div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00191">searchtree.h:191</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html_affaf44d28dc2180f93669cb8d6c09c84"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#affaf44d28dc2180f93669cb8d6c09c84">IntroductionToAlgorithm::TreeAlgorithm::SearchTree::search</a></div><div class="ttdeci">std::shared_ptr&lt; NodeType &gt; search(const T &amp;value, std::shared_ptr&lt; NodeType &gt; node=std::shared_ptr&lt; NodeType &gt;())</div><div class="ttdoc">search:在二叉搜索树中搜索指定内容的节点。 </div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00048">searchtree.h:48</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html_a2dfe32f3914b03ccdb22f88f24a6afdd"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a2dfe32f3914b03ccdb22f88f24a6afdd">IntroductionToAlgorithm::TreeAlgorithm::SearchTree::max</a></div><div class="ttdeci">std::shared_ptr&lt; NodeType &gt; max(std::shared_ptr&lt; NodeType &gt; node)</div><div class="ttdoc">max:在二叉搜索树中最大值的节点。 </div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00099">searchtree.h:99</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html_af0bb418896808f72bb1edee55d238d55"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#af0bb418896808f72bb1edee55d238d55">IntroductionToAlgorithm::TreeAlgorithm::SearchTree::min</a></div><div class="ttdeci">std::shared_ptr&lt; NodeType &gt; min(std::shared_ptr&lt; NodeType &gt; node)</div><div class="ttdoc">min:在二叉搜索树中最小值的节点。 </div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00075">searchtree.h:75</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_tree_algorithm_html_a4fb6d9c7d8b75a8d6ec4ee79fc0b1de5"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_tree_algorithm.html#a4fb6d9c7d8b75a8d6ec4ee79fc0b1de5">IntroductionToAlgorithm::TreeAlgorithm::transplant</a></div><div class="ttdeci">void transplant(std::shared_ptr&lt; NodeType &gt; node_src, std::shared_ptr&lt; NodeType &gt;node_dst, std::shared_ptr&lt; NodeType &gt; &amp;root)</div><div class="ttdoc">transplant：二叉树的剪切操作 </div><div class="ttdef"><b>Definition:</b> <a href="binarytree_8h_source.html#l00249">binarytree.h:249</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html_a0e767d97590d19c97d595708d4419fe1"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a0e767d97590d19c97d595708d4419fe1">IntroductionToAlgorithm::TreeAlgorithm::SearchTree::predecesor</a></div><div class="ttdeci">std::shared_ptr&lt; NodeType &gt; predecesor(std::shared_ptr&lt; NodeType &gt; node)</div><div class="ttdoc">predecesor:二叉搜索树指定节点的前驱。 </div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00161">searchtree.h:161</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree_html_aa0d43af70ef50ff59740dced2e832074"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_tree_algorithm_1_1_binary_tree.html#aa0d43af70ef50ff59740dced2e832074">IntroductionToAlgorithm::TreeAlgorithm::BinaryTree&lt; NodeType &gt;::root</a></div><div class="ttdeci">std::shared_ptr&lt; NodeType &gt; root</div><div class="ttdef"><b>Definition:</b> <a href="binarytree_8h_source.html#l00059">binarytree.h:59</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html_a0a9b2de6326042f2a1fa8a3a0def67ab"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a0a9b2de6326042f2a1fa8a3a0def67ab">IntroductionToAlgorithm::TreeAlgorithm::SearchTree::T</a></div><div class="ttdeci">NodeType::KeyType T</div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00036">searchtree.h:36</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html_a9d53e1f0b130263f2974dced515aa1f1"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html#a9d53e1f0b130263f2974dced515aa1f1">IntroductionToAlgorithm::TreeAlgorithm::SearchTree::successor</a></div><div class="ttdeci">std::shared_ptr&lt; NodeType &gt; successor(std::shared_ptr&lt; NodeType &gt; node)</div><div class="ttdoc">successor:二叉搜索树指定节点的后继节点。 </div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00127">searchtree.h:127</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_html"><div class="ttname"><a href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div><div class="ttdoc">Namespace of IntrodunctionToAlgorithm. </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00024">longest_common_subsequence.h:24</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree_html"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_tree_algorithm_1_1_search_tree.html">IntroductionToAlgorithm::TreeAlgorithm::SearchTree</a></div><div class="ttdoc">SearchTree：二叉搜索树，算法导论12章 </div><div class="ttdef"><b>Definition:</b> <a href="searchtree_8h_source.html#l00033">searchtree.h:33</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_8ed9dba4cd616b67da9b1338594e34e0.html">src</a></li><li class="navelem"><a class="el" href="dir_2a06141a7764b24a0bfc3200068bd811.html">tree_algorithms</a></li><li class="navelem"><a class="el" href="dir_8651736733b74ad481ef3c7c03f15201.html">searchtree</a></li><li class="navelem"><a class="el" href="searchtree_8h.html">searchtree.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
